{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9d3c613a",
   "metadata": {},
   "source": [
    "# 本周学习内容\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8751f31f",
   "metadata": {},
   "source": [
    "```\n",
    "stu_id = [10001,10002,10003,10004,10008,10001,10009,10010,10001]\n",
    "names = [\"Mike\", \"Mary\", \"Jan\", \"Jack\"]\n",
    "\n",
    "```\n",
    "\n",
    "* 思考，如何取出所有位置10001的index？"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3da5c845",
   "metadata": {},
   "source": [
    "## 列表的私有方法\n",
    "\n",
    "* 1.增加的方法\n",
    "   *  1.1 增加 append()  增加在list的末尾\n",
    "   \n",
    "```\n",
    "names.append(1)\n",
    "print(names)\n",
    "```\n",
    "---\n",
    "\n",
    "   * 1.2 插入 insert() 可以指定位置增加元素\n",
    "\n",
    "```\n",
    "names.insert(1,'yougege')\n",
    "print(\"insert之后的names：\",names)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c5d2ccc",
   "metadata": {},
   "source": [
    "* 2. 删除 remove  和 pop\n",
    "  * 优势：如果删除list中多个元素时，remove比较合适\n",
    "\n",
    "\n",
    "```\n",
    "Remove first occurrence of value.\n",
    "Raises ValueError if the value is not present.\n",
    "```\n",
    "   * 2.1 remove()一般情况下 使用remove先用in做判断\n",
    " \n",
    " ```\n",
    " if 10001 in names:\n",
    " names.remove(10001)\n",
    " print(\"remove删除的结果：\",names)\n",
    "```\n",
    "* 2.2 pop() 删除位置为1的这个元素\n",
    "\n",
    "```\n",
    "names.pop(-1)\n",
    "print(\"pop()方法后的names:\",names)\n",
    " ```\n",
    " \n",
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b5603974",
   "metadata": {},
   "source": [
    "* 3. index()索引 查看该元素如10001 在stu_id中第一个出现的位置索引\n",
    "\n",
    "```\n",
    "print(stu_id.index(10001))\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d09277e2",
   "metadata": {},
   "source": [
    "* 4. clear()清除所有数据\n",
    "```\n",
    "names.clear()\n",
    "print(\"清除names列表后的值：\",names)\n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d32b2a8",
   "metadata": {},
   "source": [
    "* 5.count()查看列表元素出现的个数，（简单应用）统计分数出现的次数\n",
    "```\n",
    "print(\"count方法的结果:\",stu_id.count(10001))\n",
    "print(names)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "baee7f5f",
   "metadata": {},
   "source": [
    "* 6. extend() 是给指定的list进行扩充\n",
    "```\n",
    "names.extend(stu_id)\n",
    "print(names)\n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c94649ab",
   "metadata": {},
   "source": [
    "## 列表的其他方法(Sequence Types 的一些方法)\n",
    "\n",
    "*  1. len()查看list长度\n",
    "```\n",
    "print(\"names的长度/names的列表元素个数：\",len(names))\n",
    "```\n",
    "\n",
    "* 2. in 查看元素是否存在list当中,结果为布尔值，False/True,\n",
    "  * 一般在操作list的修改和删除时会先判断\n",
    "```\n",
    "print(\"tingting\" in names)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a4679895",
   "metadata": {},
   "source": [
    "## 列表的高级方法\n",
    "\n",
    "* copy()\n",
    "* 枚举"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80f4a40a",
   "metadata": {},
   "source": [
    "### copy()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d8b6c87",
   "metadata": {},
   "source": [
    "```\n",
    "stu_id = [10001,10002,10003,10004,10008,10001,10009,10010,10001]\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "73ec0090",
   "metadata": {},
   "source": [
    "* 1. copy()很重要\n",
    "\n",
    ">**思考，赋值是真正的复制么？**\n",
    "\n",
    "> * 赋值(=) <=> copy? \n",
    "\n",
    ">**--->不等价**\n",
    "\n",
    "```\n",
    "stu_id_matedata = stu_id\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "af9de2fe",
   "metadata": {},
   "source": [
    "* 实验1：赋值\n",
    "\n",
    "```\n",
    "stu_id.pop(1)\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d298c55e",
   "metadata": {},
   "source": [
    "* 实验2 copy()\n",
    "\n",
    "```\n",
    "stu_id_matedata = stu_id.copy()\n",
    "stu_id.pop(1)\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata）\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d8938d09",
   "metadata": {},
   "source": [
    "* 实验3 切片实现复制\n",
    "\n",
    "```\n",
    "stu_id_matedata = stu_id[:]\n",
    "stu_id.pop(1)\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "922d7dfa",
   "metadata": {},
   "source": [
    "### 枚举"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0da2e519",
   "metadata": {},
   "source": [
    "```\n",
    "stu_id = [10001,10002,10003,10004,10008,10001,10009,10010,10001]\n",
    "```\n",
    "\n",
    "*  思考，如何取出所有位置10001的index？\n",
    "\n",
    "```\n",
    "for i in stu_id:     ( 循环遍历stu_id的所有内容）\n",
    "    if i == 10001:  （ 判断如果元素为10001的时候,怎么取出索引值呢？）\n",
    "        print()\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "08883a80",
   "metadata": {},
   "source": [
    "* 枚举:因为list其实不仅有values值，还有index索引，但for循环主要循环遍历其值，不遍历索引,\n",
    "  因此，有了枚举的方法，同时遍历两者\n",
    "  \n",
    "  ```\n",
    "   enumerate()\n",
    "   ```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "cef85b82",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 10001\n",
      "5 10001\n",
      "8 10001\n",
      "我们想要删除pop()的值： [0, 5, 8]\n"
     ]
    }
   ],
   "source": [
    "pop_values_list = []  # append方法经常用于新建列表\n",
    "for index,item in enumerate(stu_id):\n",
    "    # print(index,item)\n",
    "    if item == 10001:\n",
    "        print(index,item)\n",
    "        pop_values_list.append(index)\n",
    "print(\"我们想要删除pop()的值：\",pop_values_list)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
